package com.blog.admin.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.admin.mapper.PermissionMapper;
import com.blog.admin.mapper.UserMapper;
import com.blog.admin.pojo.auth.User;
import com.blog.admin.service.UserService;

import cn.hutool.core.util.StrUtil;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private PermissionMapper permissionMapper;
	
	@Override
	public Set<String> findPermissionsByUserId(int userId) {
		Set<String> permissions = permissionMapper.findRoleNameByUserId(userId);
		Set<String> result = new HashSet<>();
		for (String permission : permissions) {
			if (StrUtil.isBlank(permission)) {
				continue;
			}
			permission = StrUtil.trim(permission);
			result.addAll(Arrays.asList(permission.split("\\s*;\\s*")));
		}
		return result;
	}

	@Override
	public User getUser(User user) {
		return userMapper.getUser(user);
	}

	@Override
	public User getUserById(Integer id) {
		return userMapper.getUserById(id);
	}

	@Override
	public List<User> getUserList(String username) {
		return userMapper.getUserList(username);
	}

}
